﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace LiteFramework
{
    public sealed class SystemResources
    {

        private SystemResources()
        {
        }

        // 常量...

        /// <summary>
        /// 默认配置编码
        /// </summary>
        public const string DEFAULT_SECTION = "SYS_DEFAULT"; // "GenericSection";
        public const string DEFAULT_SECTION_NAME = "默认配置"; // "常规";

        /// <summary>
        /// 最小日期（1753-01-01）
        /// </summary>
        public readonly static DateTime MinDate = new DateTime(1753, 01, 01);
        /// <summary>
        /// 最大日期（2039-12-31）
        /// </summary>
        public readonly static DateTime MaxDate = new DateTime(9999, 12, 31); //new DateTime(9999, 12, 31, 23, 59, 59);

        /// <summary>
        /// 分页，默认分页数据量：50
        /// </summary>
        public const int DefaultPageSize = 50;

        /// <summary>
        /// 指示获取所有页信息。分页中，获取所有数据时，分页的页码[page]使用此值
        /// </summary>
        public const int AllPageNumber = int.MinValue;

        /// <summary>
        /// 指示获取最后一页信息。分页，获取最后一页数据时，分页的页码[page]使用此值
        /// </summary>
        public const int LastPageNumber = int.MaxValue;

        public const string FIX_MODEL = "Model";
        public const string FIX_CONTRACT = "Contract";
        public const string FIX_SERVICE = "Service";
        public const string FIX_COMPONENT = "Component";

        /// <summary>
        /// 默认电子邮箱后缀键码
        /// </summary>
        public const string DEFAULT_EMAIL_SUFFIX_KEY = "DEFAULT_EMAIL_SUFFIX";
        public const string DEFAULT_EMAIL_SUFFIX_VALUE = "@mail.com";
        public const string DEFAULT_EMAIL_SUFFIX_DESC = "默认电子邮箱后缀(如地址为username@163.com，则填@163.com)";
        /// <summary>
        /// 默认密码键码
        /// </summary>
        public const string DEFAULT_PASSWORD_KEY = "DEFAULT_PASSWORD";
        public const string DEFAULT_PASSWORD_VALUE = "888888";
        public const string DEFAULT_PASSWORD_DESC = "新建用户默认密码(长度不应小至6)";




        public const string NS = "http://www.hykon.cn/";
        public const string CONTEXT_TOKEN_SLOTNAME = ".Hykon_CONTEXT_TOKEN";

        /// <summary>
        /// 超级管理员代码：Administrator
        /// </summary>
        public const string ADMINISTRATOR_NAME = "Administrator";
        public const string APPROLE_NAME = "Application";
        public const string ANONYMOUS_NAME = "Anonymous";
        /// <summary>
        /// 数据分隔符号：$#$
        /// </summary>
        public const string SplitChar = "$#$";

        #region 日期时间格式

        /// <summary>
        /// MM-dd
        /// </summary>
        public const string DATE__MM_DD = "MM-dd";
        /// <summary>
        /// yyyy-MM-dd
        /// </summary>
        public const string DATE__YYYY_MM_DD = "yyyy-MM-dd";

        /// <summary>
        /// HH:mm
        /// </summary>
        public const string TIME__HH_MM = "HH:mm";
        /// <summary>
        /// HH:mm:ss
        /// </summary>
        public const string TIME__HH_MM_SS = "HH:mm:ss";

        /// <summary>
        /// MM-dd HH:mm
        /// </summary>
        public const string DATETIME__MM_DD__HH_MM = "MM-dd HH:mm";
        /// <summary>
        /// yyyy-MM-dd HH:mm
        /// </summary>
        public const string DATETIME__YYYY_MM_DD__HH_MM = "yyyy-MM-dd HH:mm";
        /// <summary>
        /// yyyy-MM-dd HH:mm:ss
        /// </summary>
        public const string DATETIME__YYYY_MM_DD__HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

        #endregion
        #region Task Code

        /// <summary>
        /// 指示未提供功能
        /// </summary>
        public const string TASK_CODE_NOT_SUPPLIED = "NotSupplied";

        // 系统设置
        /// <summary>
        /// 权限：查阅系统设置
        /// </summary>
        public const string TASK_CODE_SETTINGS_VIEW = "Settings_View";
        /// <summary>
        /// 权限：管理系统设置
        /// </summary>
        public const string TASK_CODE_SETTINGS_MANAGE = "Settings_Manage";

        // 单据编号规则
        /// <summary>
        /// 单据编号规则：查阅
        /// </summary>
        public const string TASK_CODE_DOCNORULE_VIEW = "DocNoRule_View";
        /// <summary>
        /// 单据编号规则：管理
        /// </summary>
        public const string TASK_CODE_DOCNORULE_MANAGE = "DocNoRule_Manage";

        // 操作日志
        /// <summary>
        /// 权限：查阅操作日志
        /// </summary>
        public const string TASK_CODE_LOG_VIEW = "Log_View";
        /// <summary>
        /// 权限：管理操作日志
        /// </summary>
        public const string TASK_CODE_LOG_MANAGE = "Log_Manage";

        // 公司
        /// <summary>
        /// 权限：查阅公司信息
        /// </summary>
        public const string TASK_CODE_COMPANY_VIEW = "Company_View";
        /// <summary>
        /// 权限：新建公司信息
        /// </summary>
        public const string TASK_CODE_COMPANY_ADD = "Company_Add";
        /// <summary>
        /// 权限：修改公司信息
        /// </summary>
        public const string TASK_CODE_COMPANY_EDIT = "Company_Edit";
        /// <summary>
        /// 权限：删除公司信息
        /// </summary>
        public const string TASK_CODE_COMPANY_DELETE = "Company_Delete";

        // 机构
        /// <summary>
        /// 权限：查阅机构信息
        /// </summary>
        public const string TASK_CODE_ORGANIZATION_VIEW = "Organization_View";
        /// <summary>
        /// 权限：新建机构信息
        /// </summary>
        public const string TASK_CODE_ORGANIZATION_ADD = "Organization_Add";
        /// <summary>
        /// 权限：修改机构信息
        /// </summary>
        public const string TASK_CODE_ORGANIZATION_EDIT = "Organization_Edit";
        /// <summary>
        /// 权限：删除机构信息
        /// </summary>
        public const string TASK_CODE_ORGANIZATION_DELETE = "Organization_Delete";

        //部门
        /// <summary>
        /// 权限：查阅部门信息
        /// </summary>
        public const string TASK_CODE_DEPARTMENT_VIEW = "Department_View";
        /// <summary>
        /// 权限：管理部门信息
        /// </summary>
        public const string TASK_CODE_DEPARTMENT_MANAGE = "Department_Manage";

        // 角色
        /// <summary>
        /// 权限：查阅角色信息
        /// </summary>
        public const string TASK_CODE_ROLE_VIEW = "Role_View";
        /// <summary>
        /// 权限：管理角色信息
        /// </summary>
        public const string TASK_CODE_ROLE_MANAGE = "Role_Manage";

        // 用户
        /// <summary>
        /// 权限：查阅用户信息
        /// </summary>
        public const string TASK_CODE_USER_VIEW = "User_View";
        /// <summary>
        /// 权限：管理用户信息
        /// </summary>
        public const string TASK_CODE_USER_MANAGE = "User_Manage";

        /// <summary>
        /// 权限：数据导入
        /// </summary>
        public const string TASK_CODE_DATA_IMPORT = "Data_Import";
        /// <summary>
        /// 权限：数据导出
        /// </summary>
        public const string TASK_CODE_DATA_EXPORT = "Data_Export";
        /// <summary>
        /// 权限：数据打印
        /// </summary>
        public const string TASK_CODE_DATA_PRINT = "Data_Print";
        /// <summary>
        /// 权限：能否查询下属机构信息
        /// </summary>
        public const string TASK_CODE_QUERY_SUBORGAN = "Query_Sub_Organ";
        ///// <summary>
        ///// 权限：修改用户密码
        ///// </summary>
        //public const string TASK_CODE_PASSWORD_CHANGE = "Password_Change";

        #endregion
        #region Cookie Name

        /// <summary>
        /// 上一次登陆的区域
        /// </summary>
        public const string Cookie_Name__Login__Last_Wspace = ".HYKON__LOGIN__LAST_WSPACE";
        /// <summary>
        /// 上一次登陆的用户名
        /// </summary>
        public const string Cookie_Name__Login__Last_User_Name = ".HYKON__LOGIN__LAST_USER_NAME";
        /// <summary>
        /// 上一次登陆使用安全证书登陆
        /// </summary>
        public const string Cookie_Name__Login__Last_Use_Security = ".HYKON__LOGIN__USE_SECURITY";

        #endregion


        /// <summary>
        /// 客户端IP
        /// </summary>
        public static string Host_IP
        {
            get
            {
                /*
                    Request.ServerVariables["HTTP_VIA"]
                    ServerVariables["HTTP_X_FORWARDED_FOR"]
                    Request.ServerVariables["REMOTE_ADDR"]
                    
                    值分下面几种情况:

                    一、没有使用代理服务器的情况：
                          REMOTE_ADDR = 用户的 IP
                          HTTP_VIA = 没数值或不显示
                          HTTP_X_FORWARDED_FOR = 没数值或不显示 

                    二、使用透明代理服务器的情况：Transparent Proxies
                          REMOTE_ADDR = 最后一个代理服务器 IP 
                          HTTP_VIA = 代理服务器 IP
                          HTTP_X_FORWARDED_FOR = 用户的真实 IP ，经过多个代理服务器时，这个值类似如下：203.98.182.163, 203.98.182.163, 203.129.72.215。
                  
                        这类代理服务器还是将您的信息转发给您的访问对象，无法达到隐藏真实身份的目的。

                    三、使用普通匿名代理服务器的情况：Anonymous Proxies
                          REMOTE_ADDR = 最后一个代理服务器 IP 
                          HTTP_VIA = 代理服务器 IP
                          HTTP_X_FORWARDED_FOR = 代理服务器 IP ，经过多个代理服务器时，这个值类似如下：203.98.182.163, 203.98.182.163, 203.129.72.215。

                        隐藏了您的真实IP，但是向访问对象透露了您是使用代理服务器访问他们的。 

                    四、使用欺骗性代理服务器的情况：Distorting Proxies
                          REMOTE_ADDR = 代理服务器 IP 
                          HTTP_VIA = 代理服务器 IP 
                          HTTP_X_FORWARDED_FOR = 随机的 IP ，经过多个代理服务器时，这个值类似如下：203.98.182.163, 203.98.182.163, 203.129.72.215。
                */
                string ip = string.Empty;
                if (HttpContext.Current != null &&
                    HttpContext.Current.Request != null &&
                    HttpContext.Current.Request.ServerVariables != null)
                {
                    ip = HttpContext.Current.Request.Headers["CDN-SRC-IP"];

                    if (string.IsNullOrEmpty(ip))
                    {
                        /// 代理IP, 被伪装：request.Headers.Add("X_FORWARDED_FOR", "0.0.0.0");
                        /// 多层代理将有多个IP，最前面为原始IP。
                        ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                        if (ip != null)
                            ip = ip.Split(',')[0];
                    }

                    if (string.IsNullOrEmpty(ip))
                    {
                        // 客户端IP
                        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    }

                    if (string.IsNullOrEmpty(ip))
                    {
                        ip = HttpContext.Current.Request.UserHostAddress;
                    }

                    // 强制验证Ip的有效性，避免可能的IP伪装以致SQL注入
                    if (!Regex.IsMatch(ip, @"^(0|[1-9]?|1\d\d?|2[0-4]\d|25[0-5])\.(0|[1-9]?|1\d\d?|2[0-4]\d|25[0-5])\.(0|[1-9]?|1\d\d?|2[0-4]\d|25[0-5])\.(0|[1-9]?|1\d\d?|2[0-4]\d|25[0-5])$"))
                        ip = "UN_SAFE: " + ip;
                }
                else
                {
                    ip = "UN_KNOW";
                }
                return ip;
            }
        }
        /// <summary>
        /// 客户端名称
        /// </summary>
        public static string Host_Name
        {
            get
            {
                string ip = Host_IP;
                string name = string.Empty;

                IPHostEntry host = null;
                try
                {
                    host = System.Net.Dns.GetHostEntry(ip);
                }
                catch { }

                if (host != null)
                {
                    name = host.HostName;
                }

                if (string.IsNullOrEmpty(name) &&
                    HttpContext.Current != null &&
                    HttpContext.Current.Request != null &&
                    HttpContext.Current.Request.ServerVariables != null)
                {
                    name = HttpContext.Current.Request.ServerVariables["REMOTE_HOST"];

                    if (string.IsNullOrEmpty(name))
                        name = HttpContext.Current.Request.UserHostName;
                }

                return name;
            }
        }
        /// <summary>
        /// 客户端信息。
        ///   格式：[ip name],
        ///   示例：[192.168.0.1 N3-Server]
        /// </summary>
        public static string Host_Info
        {
            get
            {
                string ip = Host_IP;
                string name = Host_Name;

                if (ip == name)
                    name = null;

                if (HttpContext.Current != null &&
                    HttpContext.Current.Request != null &&
                    HttpContext.Current.Request.ServerVariables != null &&
                    string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_VIA"])) // 使用代理
                    ip += "*";

                string info = null;
                if (string.IsNullOrEmpty(ip) && string.IsNullOrEmpty(name))
                    info = "*";
                else
                    info = string.Format("{0} {1}", ip, name).Trim();

                return string.Format("[{0}]", info);
            }
        }
    }
}